<template>
  <div class="login-conte">
    <van-nav-bar title="子陌小屋-登录" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="formLogin.mobile"
        name="formLogin.mobile"
        label="手机号"
        placeholder="手机号"
        required
        :rules="[
          { required: true, message: '请填写手机号', trigger: 'onChange' },
          {
            pattern: /^((13[0-9])|(14[5,7,9])|(15[^4])|(16[6])|(17[0,1,2,3,5,6,7,8])|(18[0-9])|(19[8,9]))\d{8}$/,
            message: '请输入正确的手机号'
          }
        ]"
      />
      <van-field
        v-model="formLogin.code"
        type="password"
        name="formLogin.code"
        label="密码"
        placeholder="密码"
        required
        :rules="[
          { required: true, message: '请填写密码', trigger: 'onChange' },
          { pattern: /^\d{6}$/, message: '请输入正确的密码' }
        ]"
      />
      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :loading="loading"
          :disabled="loading"
          loading-text="正在登录"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { Notify } from 'vant'
export default {
  name: 'Login',
  data() {
    return {
      formLogin: {
        mobile: '13838384388',
        code: '246810'
      },
      loading: false
    }
  },
  methods: {
    ...mapActions(['asyncLoginAction']),
    async onSubmit() {
      this.loading = true
      try {
        await this.asyncLoginAction(this.formLogin)
        this.loading = false
        Notify({ type: 'success', message: '登录成功' })
        // 跳转到Layout页面
        const path = this.$route.query.path // 提取未遂地址
        this.$router.replace({
          path: path || '/layout'
        })
      } catch (err) {
        this.loading = false
        console.log(err)
        Notify({ type: 'warning', message: '手机号或密码错误' })
      }
    }
  }
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background: linear-gradient(to right, #654ea3, #eaafc8);
}
.van-button {
  background: linear-gradient(to right, #fc466b, #3f5efb);
}
</style>
